//
//  SHEmoticonTools.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/30.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit
// 最大行数
let SHEmoticonMaxRow = 3
// 最大列数
let SHEmoticonMaxCol = 7
// 最多表情数
let SHEmoticonMaxCount = SHEmoticonMaxRow*SHEmoticonMaxCol - 1

class SHEmoticonTools: NSObject {
    
    static let sharedTools: SHEmoticonTools = SHEmoticonTools()
    
    lazy var emoticonBundle: NSBundle = {
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        return NSBundle(path: path)!
    }()
    
     // emoticon三维数组
    
    lazy var allEmoticons:[[[SHEmoticon]]] = {
        return[
            self.getEmoticonsGroup(self.defaultArr),
            self.getEmoticonsGroup(self.emojiArr),
            self.getEmoticonsGroup(self.lxhArr)
        
        ]
    }()
    
     // 默认表情
    lazy var defaultArr: [SHEmoticon] = {
        
        return self.getEmoticons("default/")
    }()
     // emoji表情
    
    lazy var emojiArr: [SHEmoticon] = {
        
       return self.getEmoticons("emoji/")
    }()
    
     // 浪小花表情
    
    lazy var lxhArr: [SHEmoticon] = {
        
        return self.getEmoticons("lxh/")
    }()
     //一维数组转二维数组
    
    func getEmoticonsGroup(emoticons: [SHEmoticon])->[[SHEmoticon]]{
        
        let pageCount = (emoticons.count + SHEmoticonMaxCount - 1) / SHEmoticonMaxCount
        var tempArr: [[SHEmoticon]] = [[SHEmoticon]]()
        
        for i in 0..<pageCount{
            
            let location = i
            
            var length = SHEmoticonMaxCount
            
            if location + length > emoticons.count{
                
                length = emoticons.count - location
            }
            
            let range = NSRange(location:location,length: length)
            
            let array = (emoticons as NSArray).subarrayWithRange(range) as! [SHEmoticon]
            
            tempArr.append(array)
        }
        
        return tempArr
    }
    
    
    // 通过读取plist文件 获取一维数组
    func getEmoticons(name: String)->[SHEmoticon]{
        
        let path = emoticonBundle.pathForResource("\(name)info.plist", ofType: nil)!
        let array = NSArray(contentsOfFile: path)!
        
        var tempArr: [SHEmoticon] = [SHEmoticon]()
        
        for dict in array{
            
            let emoticon = SHEmoticon(dict: dict as! [String: AnyObject])
            emoticon.path = "\(name)\(emoticon.png ?? "")"
            tempArr.append(emoticon)
        }
        
        return tempArr
    }
    
    func searchEmoticons(chs: String?) -> SHEmoticon?{
        
        for emoticon in defaultArr{
            
            if emoticon.chs == chs {
                
                return emoticon
            }
        }
        
        for emoticon in lxhArr{
            
            if emoticon.chs == chs {
                
                return emoticon
            }
        }
        
        return nil
        
    }
    

}
